package com.gylang.spring.netty.util;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author gylang
 * data 2021/5/21
 */
public class SpringUtils extends SpringUtil {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        super.setApplicationContext(applicationContext);
    }

    public static <T> List<T> getBeanList(Class<?> type) {
        return new ArrayList<T>((Collection<? extends T>) getBeansOfType(type).values());
    }

    public static <T> List<T> getBeanListNoCheck(Class<?> type) {

        try {
            return getBeanList(type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }
    public static <T> T getBeanNoCheck(Class<?> type) {

        try {
            return (T) getBean(type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }public static <T> T getBeanNoCheck(String name, Class<?> type) {

        try {
            return (T) getBean(name, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }public static <T> T getBeanNoCheck(String name) {

        try {
            return (T) getBean(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
